package com.soujiayi.adapter;

import java.util.List;

import com.soujiayi.activity.R;
import com.soujiayi.model.Category;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ProductClassifyExpandableListAdapter extends BaseExpandableListAdapter {

	private LayoutInflater mInflater;
	private List<?> group;
	private List<? extends List<?>> child;


	public ProductClassifyExpandableListAdapter(Context context, List<?> group, List<? extends List<?>> child) {
		this.group = group;
		this.child = child;
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return child.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		
		View view = convertView;
		ChildViewHolder holder;
		
		if (view == null) {
			view = mInflater.inflate(R.layout.product_classify_child, null);
			holder = new ChildViewHolder();
			holder.childName = (TextView) view.findViewById(R.id.product_classify_third_tile);
			view.setTag(holder);
		}else{
			holder = (ChildViewHolder)view.getTag();
		}
		bindChildView(holder, groupPosition, childPosition);


		return view;
	}

	private void bindChildView(ChildViewHolder holder, int groupPosition, int childPosition) {
		Category item = (Category) child.get(groupPosition).get(childPosition);
		holder.childName.setText(item == null ? "" : item.getName());
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return child.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return group.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return group.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		View view = convertView;
		GroupViewHolder holder;
		if (view == null) {
			view = mInflater.inflate(R.layout.product_classify_group, null);
			holder = new GroupViewHolder();
			holder.groupName = (TextView) view.findViewById(R.id.product_classify_second_title);
			holder.groupImage = (ImageView) view.findViewById(R.id.product_classify_second_title_img);
			view.setTag(holder);
		} else {
			holder = (GroupViewHolder) view.getTag();
		}
		bindGroupView(holder, groupPosition, isExpanded);
		
		return view;
	}

	private void bindGroupView(GroupViewHolder holder, int groupPosition, boolean isExpanded) {
		Object item = group.get(groupPosition);
		holder.groupName.setText(item == null ? "" : item.toString());
		if (isExpanded) {
			holder.groupImage.setImageResource(R.drawable.arrow_up);
		} else {
			holder.groupImage.setImageResource(R.drawable.arrow_down);
		}
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	private static class GroupViewHolder {
		TextView groupName;
		ImageView groupImage;
	}

	private static class ChildViewHolder {
		TextView childName;
	}


}
